<?php

// +-----------------------------------------------------------------------
// | @Copyright (c) 2012 http://t00ls.net.
// +-----------------------------------------------------------------------
// | @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-----------------------------------------------------------------------
// | @author: lenush <jnicklasj@gmail.com> qq:707207845
// +-----------------------------------------------------------------------

final class Gos_Controller_System extends Gospel_Controller_Api {

  public function unitTest() {

  }

  /**
   * 首页显示课程
   * @return void
   */
  public function index() {
    $course = new Gos_Model_Course();
    $cwhere = 'isdestory=0';
    $coursedata = $course->getItems($cwhere, 'id');
    $grade = new Gos_Model_Grade();
    $gwhere = '1=1';
    $gradedata = $grade->getItems($gwhere, 'id');
    $employee = new Gos_Model_Account();
    $ewhere = 'isdestory=0';
    $employeedata = $employee->getItems($ewhere, 'id');
    $action = new Gos_Model_Action();
    $awhere = '1=1';
    $actiondata = $action->getItems($awhere,'id');
    $account = new Gos_Model_Account();
    $accwhere = '1=1';
    $accountdata = $account->getItems($accwhere,'id');
    $this->tpl->assign('course', $coursedata);
    $this->tpl->assign('grade', $gradedata);
    $this->tpl->assign('employee', $employeedata);
    $this->tpl->assign('action', $actiondata);
    $this->tpl->assign('account', $accountdata);
    $this->tpl->draw('system/index');
  }

  public function show() {

  }

  public function news() {

  }

  public function create() {
    $return = true;
    $array = null;
    $module = "Gos_Model_" . ucfirst($this->getRequest()->getPost('doaction'));
    switch ($this->getRequest()->getPost('doaction')) {
      case 'course':
        $array['cName'] = $this->getRequest()->getPost('cName');
        $array['cTotalfee'] = $this->getRequest()->getPost('cTotalfee');
        $array['cMemo'] = $this->getRequest()->getPost('cMemo');
        break;

      case 'grade':
        $array['gname'] = $this->getRequest()->getPost('gname');
        $array['gteacher'] = $this->getRequest()->getPost('gteacher');
        $array['gmemo'] = $this->getRequest()->getPost('gmemo');
        $array['gopen'] = strtotime($this->getRequest()->getPost('gopen'));
        $array['gclass'] = $this->getRequest()->getPost('gclass');
        break;

      case 'account':
        $array['gid'] = $this->getRequest()->getPost('gid');
        $array['login'] = $this->getRequest()->getPost('login');
        $array['nick'] = $this->getRequest()->getPost('nick');
        $array['passwd'] = md5(strtolower($this->getRequest()->getPost('passwd')));
        $array['esex'] = $this->getRequest()->getPost('esex');
        $array['efrom'] = $this->getRequest()->getPost('efrom');
        $array['emarriage'] = $this->getRequest()->getPost('gid');
        $array['ebirth'] = $this->getRequest()->getPost('ebirth');
        $array['ecardid'] = $this->getRequest()->getPost('ecardid');
        $array['egraduate'] = $this->getRequest()->getPost('egraduate');
        $array['esubject'] = $this->getRequest()->getPost('esubject');
        $array['egrade'] = $this->getRequest()->getPost('egrade');
        $array['eaddress'] = $this->getRequest()->getPost('eaddress');
        $array['etelephone'] = $this->getRequest()->getPost('etelephone');
        $array['econtact'] = $this->getRequest()->getPost('econtact');
        $array['ehere'] = $this->getRequest()->getPost('ehere');
        $array['edept'] = $this->getRequest()->getPost('edept');
        $array['edeptwork'] = $this->getRequest()->getPost('edeptwork');
        break;
      default:
        $array = null;
    }
    try {
      $module = new $module();
      if (is_array($array) && false == $module->append($array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据新增成功，3秒后返回！', '/System/index');
    }
  }

  public function edit() {
    $return = null;
    $id = $this->getRequest()->getInt('parameters');
    $where = sprintf("id=%d", $id);
    try {
      $module = new Gos_Model_Course();
      $return = $module->getItem($where);
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    $this->tpl->assign('course', $return);
    $this->tpl->draw('system/courseedit');
  }

  public function emptedit() {
    $return = null;
    $id = $this->getRequest()->getInt('parameters');
    $where = sprintf("id=%d", $id);
    try {
      $module = new Gos_Model_Account();
      $return = $module->getItem($where);
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    $this->tpl->assign('employee', $return);
    $this->tpl->draw('system/emptedit');
  }

  public function grdedit() {
    $return = null;
    $id = $this->getRequest()->getInt('parameters');
    $where = sprintf("id=%d", $id);
    try {
      $module = new Gos_Model_Grade();
      $return = $module->getItem($where);
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    $this->tpl->assign('grade', $return);
    $this->tpl->draw('system/grdedit');
  }

  public function update() {
    $return = true;
    $array = null;
    $array['cName'] = $this->getRequest()->getPost('cName');
    $array['cTotalfee'] = $this->getRequest()->getPost('cTotalfee');
    $array['cMemo'] = $this->getRequest()->getPost('cMemo');
    try {
      $where = sprintf("id='%d'", $this->getRequest()->getPost('id'));
      $module = new Gos_Model_Course();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据修改成功，3秒后返回！', '/System/index');
    }
  }

  public function emptupdate() {
    $return = true;
    $array = null;
    $array['gid'] = $this->getRequest()->getPost('gid');
    $array['login'] = $this->getRequest()->getPost('login');
    $array['nick'] = $this->getRequest()->getPost('nick');
    if ($this->getRequest()->getPost('passwd') != '') {
      $array['passwd'] = md5(strtolower($this->getRequest()->getPost('passwd')));
    }
    $array['esex'] = $this->getRequest()->getPost('esex');
    $array['efrom'] = $this->getRequest()->getPost('efrom');
    $array['emarriage'] = $this->getRequest()->getPost('gid');
    $array['ebirth'] = strtotime($this->getRequest()->getPost('ebirth'));
    $array['ecardid'] = $this->getRequest()->getPost('ecardid');
    $array['egraduate'] = $this->getRequest()->getPost('egraduate');
    $array['esubject'] = $this->getRequest()->getPost('esubject');
    $array['egrade'] = $this->getRequest()->getPost('egrade');
    $array['eaddress'] = $this->getRequest()->getPost('eaddress');
    $array['etelephone'] = $this->getRequest()->getPost('etelephone');
    $array['econtact'] = $this->getRequest()->getPost('econtact');
    $array['ehere'] = strtotime($this->getRequest()->getPost('ehere'));
    $array['edept'] = $this->getRequest()->getPost('edept');
    $array['edeptwork'] = $this->getRequest()->getPost('edeptwork');
    $array['status'] = $this->getRequest()->getPost('status');
    try {
      $where = sprintf("id='%d'", $this->getRequest()->getPost('id'));
      $module = new Gos_Model_Account();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据修改成功，3秒后返回！', '/System/index');
    }
  }

  public function grdupdate() {
    $return = true;
    $array = null;
    $array['status'] = $this->getRequest()->getPost('status');
    $array['gname'] = $this->getRequest()->getPost('gname');
    $array['gteacher'] = $this->getRequest()->getPost('gteacher');
    $array['gmemo'] = $this->getRequest()->getPost('gmemo');
    $array['gopen'] = strtotime($this->getRequest()->getPost('gopen'));
    $array['gclass'] = $this->getRequest()->getPost('gclass');
    try {
      $where = sprintf("id='%d'", $this->getRequest()->getPost('id'));
      $module = new Gos_Model_Grade();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据修改成功，3秒后返回！', '/System/index');
    }
  }

  public function destory() {
    $return = true;
    try {
      $where = sprintf("id='%d'", $this->getRequest()->getInt('parameters'));
      $array['isdestory'] = 1;
      $module = new Gos_Model_Course();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据删除成功，3秒后返回！', '/System/index');
    }
  }

  public function emptdestory() {
    $return = true;
    try {
      $where = sprintf("id='%d'", $this->getRequest()->getInt('parameters'));
      $array['isdestory'] = 1;
      $module = new Gos_Model_Account();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据删除成功，3秒后返回！', '/System/index');
    }
  }

  public function grddestory() {
    $return = true;
    try {
      $where = sprintf("id='%d'", $this->getRequest()->getInt('parameters'));
      $array['isdestory'] = 1;
      $module = new Gos_Model_Grade();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('数据删除成功，3秒后返回！', '/System/index');
    }
  }

  public function dogrant(){
    $return = true;
    $array = null;
    $array['eid'] = $this->getRequest()->getPost('eid');
    $array['aids'] = implode(',',$_POST['aids']);
    try {
      $where = sprintf("eid='%d'", $this->getRequest()->getPost('eid'));
      $module = new Gos_Model_Resource();
      if (false == $module->alter($where, $array)) {
        $return = false;
      }
    } catch (Gospel_Db_Exception $mde) {
      throw $mde;
    }
    if ($return) {
      $this->message('用户授权成功，3秒后返回！', '/System/index');
    }

  }

}

/**
 * // +---------------------------------------------------------------------
 * // | @ Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * // +---------------------------------------------------------------------
 * // | @ author: lenush <jnicklasj@gmail.com> qq:707207845
 * // +---------------------------------------------------------------------
 * Local variables:
 * tab-width:4
 * basic-offset:4
 * indent-tabs-mode:t
 * End:
 */
